package com.youtellv1.mapper;

import com.youtellv1.entity.TeacherSign;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

@Mapper
public interface TeacherSignMapper extends BaseMapper<TeacherSign>{
    // 查询某教师指定日期是否已签到
    @Select("SELECT COUNT(*) FROM tb_teachersign WHERE tid = #{tid} AND sdate = #{sdate}")
    int countByTidAndDate(@Param("tid") Integer tid, @Param("sdate") Date sdate);

    // 插入签到记录（sdate, tid）
    @Insert("INSERT INTO tb_teachersign (sdate, tid) VALUES (#{sdate}, #{tid})")
    void insertSignRecord(TeacherSign teacherSign);

    // 查询某教师所有签到日期，按日期升序
    @Select("SELECT sdate FROM tb_teachersign WHERE tid = #{tid} ORDER BY sdate ASC")
    List<Date> findAllSignDates(@Param("tid") Integer tid);

    // 查询某教师当月签到日期（yyyy-MM格式匹配）
    @Select("SELECT sdate FROM tb_teachersign WHERE tid = #{tid} AND DATE_FORMAT(sdate, '%Y-%m') = #{yearMonth} ORDER BY sdate ASC")
    List<Date> findThisMonthSignDates(@Param("tid") Integer tid, @Param("yearMonth") String yearMonth);

    // 查询教师当天是否签到
    @Select("SELECT COUNT(*) FROM tb_teachersign WHERE tid = #{tid} AND sdate = CURDATE()")
    int countTodaySign(@Param("tid") Integer tid);

    // 查询所有签到记录
    @Select("SELECT * FROM tb_teachersign")
    List<TeacherSign> findAllTeacherSign();

    // 根据教师id查询签到记录（多条）
    @Select("SELECT * FROM tb_teachersign WHERE tid = #{tid}")
    List<TeacherSign> findByTid(@Param("tid") Integer tid);

    // 根据教师id和日期查询签到记录（单条）
    @Select("SELECT * FROM tb_teachersign WHERE tid = #{tid} AND sdate = #{sdate}")
    TeacherSign findByTidAndDate(@Param("tid") Integer tid, @Param("sdate") Date sdate);

    // 更新签到记录（用script标签，判断是否更新字段）
    @Update("<script>" +
            "UPDATE tb_teachersign " +
            "<set>" +
            "  <if test='sdate != null'>sdate = #{sdate},</if>" +
            "  <if test='tid != null'>tid = #{tid}</if>" +
            "</set>" +
            "WHERE sgid = #{sgid}" +
            "</script>")
    void update(TeacherSign teacherSign);

    // 删除签到记录（根据主键sgid）
    @Delete("DELETE FROM tb_teachersign WHERE sgid = #{sgid}")
    void delete(TeacherSign teacherSign);

    // 插入签到记录，教师id和当前日期
    @Insert("INSERT INTO tb_teachersign (tid, sdate) VALUES (#{tid}, CURDATE())")
    void insert(TeacherSign teacherSign);

}